/* Created By TheBrodieMan */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using CommonBehaviors.Actions;
using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.Profiles;
using Styx.Pathing;
using Styx.TreeSharp;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

using Action = Styx.TreeSharp.Action;

namespace Honorbuddy.Quest_Behaviors.SpecificQuests.Vortex
{
	[CustomBehaviorFileName(@"SpecificQuests\Kalimdor\Ashenvale\13888-Vortex")]
	public class Vortex : CustomForcedBehavior
	{
		public Vortex(Dictionary<string, string> args)
			: base(args)
		{
			try
			{
				QuestId = 13888;//GetAttributeAsQuestId("QuestId", true, null) ?? 0;
			}
			catch
			{
				Logging.Write("Problem parsing a QuestId in behavior: Vortex");
			}
		}
		public int QuestId { get; set; }
		private bool _isBehaviorDone;
		public int LordMagId = 34295;
		private Composite _root;
		public QuestCompleteRequirement questCompleteRequirement = QuestCompleteRequirement.NotComplete;
		public QuestInLogRequirement questInLogRequirement = QuestInLogRequirement.InLog;
		static public bool InVehicle { get { return Lua.GetReturnVal<int>("if IsPossessBarVisible() or UnitInVehicle('player') or not(GetBonusBarOffset()==0) then return 1 else return 0 end", 0) == 1; } }
		public override bool IsDone
		{
			get
			{
				return _isBehaviorDone;
			}
		}
		
		private LocalPlayer Me
		{
			get { return (StyxWoW.Me); }
		}

		public override void OnStart()
		{
			OnStart_HandleAttributeProblem();
			if (!IsDone)
			{
				if (TreeRoot.Current != null && TreeRoot.Current.Root != null && TreeRoot.Current.Root.LastStatus != RunStatus.Running)
				{
					var currentRoot = TreeRoot.Current.Root;
					if (currentRoot is GroupComposite)
					{
						var root = (GroupComposite)currentRoot;
						root.InsertChild(0, CreateBehavior());
					}
				}
				PlayerQuest Quest = StyxWoW.Me.QuestLog.GetQuestById((uint)QuestId);
				TreeRoot.GoalText = ((Quest != null) ? ("\"" + Quest.Name + "\"") : "In Progress");
			}
		}

		public WoWUnit LordMagmathar
		{
			get
			{
				return ObjectManager.GetObjectsOfType<WoWUnit>().Where
					(u => u.Entry == LordMagId && u.IsAlive && u.Distance < 1000).
					OrderBy(u => u.Distance).FirstOrDefault();
			}
		}

		public bool IsQuestComplete()
		{
			var quest = StyxWoW.Me.QuestLog.GetQuestById((uint)QuestId);
			return quest == null || quest.IsCompleted;
		}

		public Composite DoneYet
		{
			get
			{
				return
					new Decorator(ret => IsQuestComplete(), new Action(delegate
					{
						TreeRoot.StatusText = "Finished!";
						if (InVehicle)
							Lua.DoString("CastPetAction(5)");
						_isBehaviorDone = true;
						return RunStatus.Success;
					}));
			}
		}

		public Composite KillMag
		{
			get
			{
				return new Decorator(context => LordMagmathar != null && LordMagmathar.Location.Distance(Me.Location) <= 100,
						new Action(context =>
						{
							LordMagmathar.Target();
							Lua.DoString("CastPetAction(1)");
							Lua.DoString("CastPetAction(2)");
							Lua.DoString("CastPetAction(3)");
							return RunStatus.Success;
						}));
			}
		}

		protected override Composite CreateBehavior()
		{
			return _root ?? (_root = new Decorator(ret => !_isBehaviorDone,
				new PrioritySelector(
					new Decorator(context => !InVehicle,
						new Action(context => { _isBehaviorDone = true; })),
					DoneYet,
					KillMag,
					new ActionAlwaysSucceed())));
		}
	}
}